﻿using System.Collections.Generic;

namespace Dexter.Domain.Results {
	/// <summary>
	/// The implementation of <see cref="IPagedResult{T}"/>
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public class PagedResult <T> : IPagedResult <T> {
		
		/// <summary>
		/// Initializes a new instance of the <see cref="PagedResult&lt;T&gt;"/> class.
		/// </summary>
		/// <param name="pageIndex">Index of the page.</param>
		/// <param name="pageSize">Size of the page.</param>
		/// <param name="result">The result.</param>
		/// <param name="totalCount">The total count.</param>
		public PagedResult ( int pageIndex , int pageSize , IEnumerable <T> result , long totalCount ) {
			PageIndex = pageIndex;
			PageSize = pageSize;
			Result = result;
			TotalCount = totalCount;
		}

		#region Implementation of IPagedResults<T>

		/// <summary>
		/// Gets or sets the index of the page.
		/// </summary>
		/// <value>The index of the page.</value>
		public int PageIndex { get; private set; }

		/// <summary>
		/// Gets or sets the size of the page.
		/// </summary>
		/// <value>The size of the page.</value>
		public int PageSize { get; private set; }

		/// <summary>
		/// Gets or sets the result.
		/// </summary>
		/// <value>The result.</value>
		public IEnumerable <T> Result { get; private set; }

		/// <summary>
		/// Gets or sets the total count.
		/// </summary>
		/// <value>The total count.</value>
		public long TotalCount { get; private set; }

		/// <summary>
		/// Gets the total pages.
		/// </summary>
		/// <value>The total pages.</value>
		public int TotalPages {
			get {
				return ( int )( TotalCount / PageSize ) + ( TotalCount % PageSize == 0
																		? 0
																		: 1 );
			}
		}

		/// <summary>
		/// Gets a value indicating whether this instance has previous page.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance has previous page; otherwise, <c>false</c>.
		/// </value>
		public bool HasPreviousPage {
			get { return ( PageIndex > 0 ); }
		}

		/// <summary>
		/// Gets a value indicating whether this instance has next page.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance has next page; otherwise, <c>false</c>.
		/// </value>
		public bool HasNextPage {
			get { return ( PageIndex * PageSize ) <= TotalCount; }
		}

		#endregion
	}
}
